/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: UserController
 * Author:   zhouheng
 * Date:     2019/3/29 19:24
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.onesquare.user.controller;

import com.onesquare.user.client.AuthServiceClient;
import com.onesquare.user.pojo.User;
import com.onesquare.user.service.UserService;
import com.onesquare.user.util.RedisUtil;
import com.onesquare.common.entity.Result;
import com.onesquare.common.entity.StatusCode;
import com.util.JwtUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author zhouheng
 * @create 2019/3/29
 * @since 1.0.0
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private AuthServiceClient client;

    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping(value = "login")
    public Result login(@RequestBody User user){
        user = userService.login(user.getMobile(), user.getPassword());

        // 获取token
//        Jwt jwt = client.getToken("basic b3MtdXNlcjoxMjM0NTY","password",
//                user.getMobile(), user.getPassword());
//        if (jwt == null){
//            throw new UserLoginException("error internal");
//        }

        // 生成jwt
        String token = jwtUtil.createJWT(user.getId(),user.getMobile(),"user");
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("roles", "user");

        return new Result(true,StatusCode.OK,"登陆成功", map);
    }

    @ApiOperation(value = "用户注册", notes = "code,user必选项")
    @PostMapping(value = "/register/{code}")
    public Result register(@PathVariable String code, @RequestBody User user){
        // TODO 1.判断手机号是否已注册
        // 2.得到缓存中的验证码
        String checkCode = (String) redisUtil.get("checkCode_"+code);
        if (StringUtils.isEmpty(checkCode)) {
            return new Result(false, StatusCode.ERROR, "请先获取手机验证码");
        }
        if(StringUtils.equals(checkCode,code)){
            return new Result(false, StatusCode.ERROR, "请输入正确的验证码");
        }

        userService.add(user);

        return new Result(true, StatusCode.OK, "注册成功");
    }

    /**
     * @description: 发送短信
     * @date: 2019/3/29 19:31
     * @author:zhouheng
     */
    @ApiOperation(value = "发送短信", notes = "phoneCode为必选项")
    @PostMapping(value = "/sendSms/{mobile}")
    public Result sendSms(@PathVariable String mobile){
        // 1.手机号码校验
        // 2. 调用短信服务发送短信
        userService.sendSms(mobile);

        return new Result(true, StatusCode.OK, "发送成功");
    }

    @PostMapping(value = "hello")
    public String hello(){
        return "hello world";
    }

}